const express = require("express");
const bodyParser = require("body-parser");
let static = express.static;

let { expressjwt } = require("express-jwt");
let userRouter = require("./routers/user")
let flagRouter = require("./routers/flag")
let commentRouter = require("./routers/comments")
let sceneryRouter = require("./routers/scenery")

let app = express();

app.use(function (req, res, next) {
    if (req.path !== "/") {
        next();
    }
})
app.use(bodyParser.json());
app.use(static(__dirname + "/static"))

app.use(expressjwt({
    secret: "mysecret",
    algorithms: ["HS256"],
}).unless({
    path: ["/user/login", "/user/registry"]
}))



app.use("/user", userRouter)
app.use("/flag", flagRouter);
app.use("/comments", commentRouter)
app.use("/scenery", sceneryRouter)

app.use(function (err, req, res, next) {
    if (err) {

        // if (err.name === "UnauthorizedError") {
        //     return res.status(401).json({ info: "token错误" })
        // }
        res.status(500).json({
            status: 500,
            message: '未知的错误',
        })
    }
})


app.listen(8989);